namespace Brjnk.Maps
{
    using System;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;

    /// <summary>
    /// </summary>
    public static class TileGenerator
    {
        private static Lazy<TileTemplate> template = new Lazy<TileTemplate>();

        public static BitmapSource Generate(TileGeneratorArgs args, int pixelWidth, int pixelHeight, double dpi)
        {
            template.Value.DataContext = args;
            var size = new Size(pixelWidth / dpi * 96, pixelHeight / dpi * 96);
            template.Value.Measure(size);
            template.Value.Arrange(new Rect(size));
            template.Value.UpdateLayout();
            RenderTargetBitmap target = new RenderTargetBitmap(pixelWidth, pixelHeight, dpi, dpi, PixelFormats.Pbgra32);
            target.Render(template.Value);
            return target;
        }
    }
}